﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Xml;

namespace LiuYan.Interface
{
   public class WxPay
    {



        /// <summary>
        /// 统一支付接口
        /// </summary>
        public string TyPay(string orderno,string openid,int price)
        {
           
                Dictionary<string, string> sParams1 = new Dictionary<string, string>();
                sParams1.Add("appid", WxEntity.appid);//订单信息
                sParams1.Add("attach", "attach");
                sParams1.Add("body", orderno);//订单信息
                sParams1.Add("mch_id", WxEntity.mch_id);//商户号
                sParams1.Add("nonce_str", new Common().GenerateNonceStr());//随机数
                //  sParams1.Add("device_info", "WEB");//终端设备号(门店号或收银设备ID)，注意：PC网页或公众号内支付请传"WEB"
                sParams1.Add("notify_url", WxEntity.notifyUrl);//notify_url  通知地址
                sParams1.Add("out_trade_no", orderno);//out_trade_no   商户系统内部的订单号
                sParams1.Add("spbill_create_ip", HttpContext.Current.Request.UserHostAddress);//支付的ip地址
                sParams1.Add("total_fee", price.ToString());//total_fee    总金额（分）
                //  sParams1.Add("detail", "detail");
                //  sParams1.Add("fee_type", "CNY");
                sParams1.Add("trade_type", "JSAPI");//trade_type   //交易类型  
                sParams1.Add("openid", openid);
                sParams1.Add("sign", new Common().GetSign(sParams1, WxEntity.key));//微信签名

                WebClient wc = new WebClient();//将xml数据post给微信统一下单接口
                string xml = new Common().CreateXML(sParams1);
                string returnmsg = wc.UploadString(WxEntity.UnifiedPayUrl, "post", xml);//微信返回xml
                XmlDocument doc1 = new XmlDocument();
                doc1.LoadXml(returnmsg);
                XmlElement root1 = doc1.DocumentElement;
                int len1 = root1.ChildNodes.Count;
                var prepay_id = "";
                for (int i = 0; i < len1; i++)
                {
                    string key1 = root1.ChildNodes[i].Name;
                    string value = root1.ChildNodes[i].InnerText.Trim();
                    if (key1 == "prepay_id")
                    {
                        prepay_id = value;
                    }
                }
                return prepay_id;

        }

       /// <summary>
       /// 取消订单
       /// </summary>
       /// <param name="orderno"></param>
       /// <returns></returns>
       public string CloseOrder(string orderno)
       {
           Dictionary<string, string> sParams1 = new Dictionary<string, string>();
           sParams1.Add("appid", WxEntity.appid);//订单信息
           sParams1.Add("mch_id", WxEntity.mch_id);//商户号
           sParams1.Add("out_trade_no", orderno);//out_trade_no   商户系统内部的订单号
           sParams1.Add("nonce_str", new Common().GenerateNonceStr());//随机数
           sParams1.Add("sign", new Common().GetSign(sParams1, WxEntity.key));//微信签名
           WebClient wc = new WebClient();//将xml数据post给微信统一下单接口
           string xml = new Common().CreateXML(sParams1);
           return wc.UploadString(WxEntity.closeOrderUrl, "post", xml);//微信返回xml
       }



    }
}
